﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NawaData.Dto;

namespace NawaData.Dao
{
    public class GiDao
    {
        public List<ConfirmQty> getGiByOrderMatNo(string OrderNo, string MatNo)
        {
            List<ConfirmQty> confirmQtys = new List<ConfirmQty>();
            using (var ctx = new nawaDbEntities())
            {
                var billByOrderMat = (from bill in ctx.vmsa593_billdata_0280
                                      where bill.mcustpono.ToLower() == OrderNo && bill.mmatno == MatNo && bill.mbilltype == "ZZE1"
                                      orderby bill.mgidate
                                      select bill).ToList();
                var saleByOrderMat = (from sale in ctx.vmsa551_saleorder_daily
                                      where sale.mpono.ToLower() == OrderNo && sale.mmatno == MatNo && (sale.mstatus.ToLower() == "n" || sale.mstatus.ToLower() == "c")
                                      select sale).ToList();

                foreach (var bill in billByOrderMat)
                {
                    foreach (var sale in saleByOrderMat)
                    {
                        if (bill.mcustpono.Trim().ToLower() == sale.mpono.Trim().ToLower())
                        {
                            var confirmQty = new ConfirmQty()
                            {
                                ActualQty = sale.mactqty ?? 0,
                                FirmQty = sale.mfirmqty ?? 0,
                                OrderNo = sale.mordno,
                                GiQty = bill.mbaseqty,
                                GiDate = bill.mgidate.ToString() ?? ""
                            };
                            confirmQtys.Add(confirmQty);
                        }
                    }
                }
                if (confirmQtys.Count == 0 && saleByOrderMat.Count > 0)
                {
                    foreach (var sale in saleByOrderMat)
                    {
                        var confirmQty = new ConfirmQty()
                        {
                            ActualQty = sale.mactqty ?? 0,
                            FirmQty = sale.mfirmqty ?? 0,
                            OrderNo = sale.mordno,
                            GiQty = 0,
                            GiDate = ""
                        };
                        confirmQtys.Add(confirmQty);
                    }
                }
                decimal sumGiQty = confirmQtys.Sum(gi => gi.GiQty);
                foreach (var c in confirmQtys)
                {
                    if (c.ActualQty - c.FirmQty == 0 && c.ActualQty - sumGiQty == 0)
                    {
                        c.Status = "Complete";
                    }
                    else
                    {
                        c.Status = "Not Complete";
                    }
                }

                return confirmQtys;
            }
        }


    }
}
